package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.LoginDTO;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.ex.CustomerException;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.sms.SendSmsUtil;
import com.itheima.reggie.utils.RedisUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * 用户控制层
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SendSmsUtil sendSmsUtil;

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

   /* *//**
     * 发送验证码
     * @param session
     * @param phone
     * @return
     * @throws Exception
     *//*
    @GetMapping("/sms/send")
    public R send(HttpSession session, String phone) throws Exception {
        String code = ValidateCodeUtils.generateCode(4);

        session.setAttribute("app_login_codeL:" + phone, code);

        // sendSmsUtil.sendMessage(phone, code, "SMS_254910142");
        return R.success(code);
    }*/

    /**
     * 发送验证码
     * @param loginDTO
     * @return
     * @throws Exception
     */
    @GetMapping("/sms/send")
    public R send(LoginDTO loginDTO) throws Exception {
        // 1.生成验证码并保存redis
        String code = ValidateCodeUtils.generateCode(4);
        // 2.创建redis的key
        String key = RedisUtils.createKey("app_login_codeL:", loginDTO.getPhone());

        // 存入redis，指定过期时间为5分钟
        stringRedisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);

        // 2.发送验证码
        return R.success(code);
    }


/*    *//**
     * 用户登录
     *
     * @return
     *//*
    @PostMapping("/login")
    public R userLogin(HttpSession session, @RequestBody LoginDTO loginDTO) {
        long userId = userService.login(loginDTO, (String) session.getAttribute("app_login_codeL:" + loginDTO.getPhone()));
        // 登录成功，把用户ID放进去
        session.setAttribute("user",userId);

        return R.success("用户登录成功");
    }*/

    @PostMapping("/login")
    public R userLogin(HttpSession session, @RequestBody LoginDTO loginDTO) {
        // 1.获取验证码
        // 1.1创建redis的key
        String key = RedisUtils.createKey("app_login_codeL:", loginDTO.getPhone());
        // 1.2获取redis中的验证码
        String redisCode = stringRedisTemplate.opsForValue().get(key);
        // 2.登录
        long userId = userService.login(loginDTO, redisCode);
        // 登录成功
        session.setAttribute("user",userId);
        // 3.登录成功，删除验证码
        // 清空redis中的前台登录功能的验证码
        stringRedisTemplate.delete(key);
        return R.success("用户登录成功");
    }
}